import 'dotenv/config';
import type { VercelRequest, VercelResponse } from '@vercel/node';
import axios from 'axios';

const { NOWAPI_URL, NOWAPI_SIGN, NOWAPI_SIGN_APPKEY } = process.env;

export default async (request: VercelRequest, response: VercelResponse) => {
   const cityName = request.query.cityName;
   if (!cityName)
      return response.status(400)
         .json({ messsage: "the value of【cityName】is empty" }).end();

   let url = `${NOWAPI_URL}?app=weather.pm25&cityNm=${cityName}&appkey=${NOWAPI_SIGN_APPKEY}&sign=${NOWAPI_SIGN}&format=json`
   const { data: { status, ...result } } = await axios.get(url)

   console.log(`${JSON.stringify(result.result, null, 4)}`)

   return response.json({
      data: result.result
   })
}
